package com.randi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.randi.common.annotation.LogAnnotation;
import com.randi.common.core.domain.Result;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;
import com.randi.common.core.domain.entity.SysRole;
import com.randi.system.domain.params.SysRoleParam;
import com.randi.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ziye
 */
@RestController
@RequestMapping("system/role")
@Api(tags = "角色管理")
public class SysRoleController {

    @Resource
    private ISysRoleService iSysRoleService;

    /**
     * 分页获取列表
     *
     * @param sysRoleParam 检索参数
     * @return Result
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取列表", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:list')")
    public Result selectList(@RequestBody SysRoleParam sysRoleParam) {
        PageInfo<SysRole> sysRoles = iSysRoleService.selectSysRolePage(sysRoleParam);
        return Result.success().putPage(sysRoles);
    }

    /**
     * 查看详情
     *
     * @param roleId roleId
     * @return Result
     */
    @GetMapping("/detail/{roleId}")
    @ApiOperation(value = "查看详情", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:list')")
    public Result selectById(@PathVariable Long roleId) {
        SysRole sysRole = iSysRoleService.selectSysRoleById(roleId);
        return Result.success().put(sysRole);
    }

    /**
     * 插入数据
     *
     * @param sysRole 业务信息
     * @return 结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "插入数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:add')")
    @LogAnnotation(module = "角色管理", value = "添加数据", businessType = BusinessType.INSERT)
    public Result insert(@RequestBody SysRole sysRole) {
        int status = iSysRoleService.insertSysRole(sysRole);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_INSERT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_INPUT_SUCCESS);
    }

    /**
     * 更新数据
     *
     * @param sysRole 业务信息
     * @return 结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:edit')")
    @LogAnnotation(module = "角色管理", value = "更新数据", businessType = BusinessType.UPDATE)
    public Result update(@RequestBody SysRole sysRole) {
        int status = iSysRoleService.updateSysRole(sysRole);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_EDITOR_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_EDITOR_SUCCESS);
    }

    /**
     * 状态变更
     *
     * @param sysRole 业务信息
     * @return 结果
     */
    @PostMapping("/update/status")
    @ApiOperation(value = "状态变更", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:edit')")
    @LogAnnotation(module = "角色管理", value = "状态变更", businessType = BusinessType.UPDATE)
    public Result updateStatus(@RequestBody SysRole sysRole) {
        int status = iSysRoleService.updateSysRoleStatus(sysRole);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_EDITOR_SUCCESS);
        }
        return Result.success(ResultCode.BUSINESS_EDITOR_SUCCESS);
    }

    /**
     * 删除数据
     *
     * @param roleIds roleIds
     * @return 结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:delete')")
    @LogAnnotation(module = "角色管理", value = "删除数据", businessType = BusinessType.DELETE)
    public Result deleteBatch(@RequestBody List<Long> roleIds) {
        int status = iSysRoleService.deleteBatchSysRole(roleIds);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }
}
